//
//  MBProgressHUD.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/6.
//  Copyright © 2018年 common. All rights reserved.
//

import Foundation
import MBProgressHUD

extension MBProgressHUD {
    
   ///显示信息
   ///
   /// - Parameters:
   ///   - text: 信息内容
   ///   - icon: 图标
   ///   - view: 显示视图
    class func show(text:String, icon:String, view:UIView?) {
    var newView = view
    if newView == nil {
        newView = UIApplication.shared.windows.last
    }
    let hub = MBProgressHUD.showAdded(to: newView!, animated: true)
    hub.label.text = text
    hub.label.font = UIFont.systemFont(ofSize: 16 + (ceil(kSCREEN_WIDTH/375.0) - 1)*2)
    hub.contentColor = UIColor.white
    hub.bezelView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.9)
    hub.customView = UIImageView(image: UIImage(named: icon))
    hub.mode = MBProgressHUDMode.customView
    hub.removeFromSuperViewOnHide = true
    hub.hide(animated: true, afterDelay: 1.5)
  }
    
    /// 显示成功新
    ///
    /// - Parameter success: 信息内容
    class func showSuccess(success:String) {
        self.showSuccess(success: success, toView: nil)
    }
    
    
    /// 显示成功信息
    ///
    /// - Parameters:
    ///   - success: 信息内容
    ///   - view: 显示信息的视图
    class func showSuccess(success:String, toView view:UIView?) {
        self.show(text: success, icon: "", view: view)
    }
    
    /// 显示错误信息
    ///
    /// - Parameter error: 错误信息
    class func showError(error:String) {
        self.showError(error: error, toView: nil)
    }
    
    /// 显示错误信息
    ///
    /// - Parameters:
    ///   - error: 错误信息内容
    ///   - view: 需要显示信息的视图
    class func showError(error:String, toView view:UIView?) {
        self.show(text: error, icon: "", view: view)
    }
    
//    class func showMessage(message:String) -> MBProgressHUD{
//        return self.showMessage(message: message, toView: nil)
//    }
    
    class func showMessage(message:String?, toView view:UIView?){
        var newView = view
        if newView == nil {
            newView = UIApplication.shared.windows.last
        }
        let hub = MBProgressHUD.showAdded(to: view!, animated: true)
        hub.label.text = message
        hub.label.font = UIFont.systemFont(ofSize: 16 + (ceil(kSCREEN_WIDTH/375.0) - 1)*2)
        hub.contentColor = UIColor.white
        hub.bezelView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.9)
        hub.removeFromSuperViewOnHide = true
    }
    
    
    /// 手动关闭MBProgressHUD
    class func hideHUD() {
        self.hideHUDForView(view: nil)
    }
    
    
    /// 手动关闭MBProgressHUD
    ///
    /// - Parameter view:显示MBProgressHUD的视图
    class func hideHUDForView(view:UIView?) {
        var newView = view
        if newView == nil {
            newView = UIApplication.shared.windows.last
        }
        self.hide(for: newView!, animated: true)
    }
    
    
}
